package user.impl.robUser;

import enumeration.EnumRobStrategy;
import exception.LoginErrorException;
import http.util.PrintUtil;
import main.robClass;
import robber.AbstractRobber;
import robber.IRobber;
import user.IUser;
import user.impl.SimpleUserDTO;

import java.text.SimpleDateFormat;
import java.util.*;

public abstract class RobUser extends SimpleUserDTO implements IRobUser, IUser {
    /**
     * 缓存 - 所有课程列表
     */
    private HashMap<String, String> IdAndCourseNameCache = new HashMap<>();
    /**
     * 缓存 - 课程ID,课程名字
     */
    private HashMap<String, String> commonIdAndCourseNameCache = new HashMap<>();
    /**
     * 用户 - 要抢课程列表
     */
    private ArrayList<String> robClasses = new ArrayList<>();
    /**
     * 开始时间
     */
    private String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    /**
     * 废弃 - 抢课线程量
     */
    private Integer threads = 5;
    /**
     * 抢课策略
     */
    EnumRobStrategy enumRobStrategy = EnumRobStrategy.ALL;
    /**
     * 加入抢课队列 - 1
     */
    private int robEnable;
    /**
     * 获取robber抢课对象
     */
    private IRobber iRobber;

    /**
     * 显示抢课列表
     */
    @Override
    public void showRobCourses() {
        PrintUtil.printTitle("确定的抢课列表");
        for (String robClassName : getRobClasses()) {
            Optional<Map.Entry<String, String>> IdAndCourseName = ((AbstractRobber) getIRobber()).getIdAndCourseNameCache().entrySet().stream().filter(
                    course -> course.getKey().equalsIgnoreCase(robClassName)
            ).findFirst();
            if (IdAndCourseName.isPresent()) {
                robClass.logger.info(IdAndCourseName.get().getKey());
            }
        }
    }

    @Override
    public IRobber getIRobber() {
        return iRobber;
    }

    @Override
    public void setIRobber(IRobber iRobber) {
        this.iRobber = iRobber;
    }

    @Override
    public ArrayList<String> getRobClasses() {
        return robClasses;
    }

    public void setRobClasses(ArrayList<String> robClasses) {
        this.robClasses = robClasses;
    }

    @Override
    public EnumRobStrategy getEnumRobStrategy() {
        return enumRobStrategy;
    }

    @Override
    public void setEnumRobStrategy(EnumRobStrategy enumRobStrategy) {
        this.enumRobStrategy = enumRobStrategy;
    }


    @Override
    public String getStartTime() {
        return this.startTime;
    }

    @Override
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    @Override
    public void setThreads(Integer num) {
        this.threads = num;
    }

    @Override
    public Integer getThreads() {
        return this.threads;
    }

    @Override
    public int getRobEnable() {
        return robEnable;
    }

    @Override
    public void setRobEnable(Integer num) {
        this.robEnable = num;
    }

    @Override
    public String toString() {
        return "RobUser{" +
                "IdAndCourseNameCache=" + IdAndCourseNameCache +
                ", commonIdAndCourseNameCache=" + commonIdAndCourseNameCache +
                ", robClasses=" + robClasses +
                ", startTime='" + startTime + '\'' +
                ", threads=" + threads +
                ", enumRobStrategy=" + enumRobStrategy +
                ", robEnable=" + robEnable +
                ", iRobber=" + iRobber +
                "} " + super.toString();
    }

    @Override
    public void login() throws LoginErrorException {
        try {
            _login();
        } catch (Exception e) {
            e.printStackTrace();
            PrintUtil.printError(this, Thread.currentThread(), String.valueOf(e.getCause()));
            throw new LoginErrorException();
        }
    }

    protected abstract void _login() throws Exception;

}
